<?php
/**
 * File: YCLoader.php:YCMS
 * User: xiaoyan f@yanyunfeng.com
 * Date: 15-5-5
 * Time: 下午2:36
 * @Description
 */
date_default_timezone_set('PRC');
session_start();
define("APP_DIR", dirname(dirname(dirname(__FILE__))));
define("ACCESS_KEY","xiaoyan.me");
define("LIB_DIR", dirname(__FILE__));
define("LIB_VER", '1.1.1');

class YCLoader{
    private static $loader = null;
    private function YCLoader(){}
    private $includeFiles = array(
        'sys' => array(
            'Common','YC','Exceptions','Cache','Router','RouterCore','TemplateCore','Controller','DBCore'
        ),
        'lib' => array(
            'Auth','Security','Input','Config'
        )
    );

    public static function init(){
        if(self::$loader != null) return self::$loader;
        $loader = new YCLoader();
        $loader->loadLibrary();
        self::$loader = $loader;
        return $loader;
    }
    public static function loadLib($lib){
        $lib = str_replace('.','/',$lib);
        if(file_exists(LIB_DIR.'/libs/'.$lib.'.php')) {
            include_once(LIB_DIR.'/libs/'.$lib.'.php');
        }
    }
    public function loadUserLib($libs = null){
        if(!empty($libs)){
            foreach($libs as $f){
                if(file_exists($f)) {
                    include($f);
                }
            }
        }
        return $this;
    }
    public function import($files = null){
        if($files){
            if(is_string($files)) $files = array($files);

            foreach($files as $f){
                if(file_exists(LIB_DIR.'/libs/'.$f.'.php')) {
                    include_once(LIB_DIR.'/libs/'.$f.'.php');
                }
            }
        }
    }
    private function loadLibrary(){
        $libPath = array(
            'sys' => LIB_DIR.'/',
            'lib' => LIB_DIR.'/libs/',
            'twig' => LIB_DIR.'/libs/Twig/'
        );
        foreach($this->includeFiles as $k => $fs){
            foreach($fs as $f){
                if(file_exists($libPath[$k].$f.'.php')) {
                    include($libPath[$k].$f.'.php');
                }
            }
        }
    }

    public function getInput(){
        if(Cache::getInstance()->exists('Input')) return Cache::getInstance()->get('Input');
        $int = new Input(); Cache::getInstance()->set('Input',$int);
        return $int;
    }
    public function getSec(){
        if(Cache::getInstance()->exists('Security')) return Cache::getInstance()->get('Security');
        $sec = new Security(); Cache::getInstance()->set('Security',$sec);
        return $sec;
    }
}